package com.hss01248.dialog;

import com.hss01248.dialog.config.ConfigBean;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentTreeObserver;
import ohos.agp.window.service.*;

import java.util.Optional;

/**
 * Created by Administrator on 2017/12/4.
 */

public class WindowAdjuster {

    public static void adjust(Window window, ConfigBean bean){
        setGravity(window,bean);
        setIfTypeToast(window,bean);

        setStyleOnLayout(window,bean);
    }

    private static void setStyleOnLayout(final Window window, final ConfigBean bean) {
        window.getCurrentComponentFocus().get().getComponentTreeObserver().addTreeLayoutChangedListener(new ComponentTreeObserver.GlobalLayoutListener() {
            @Override
            public void onGlobalLayoutUpdated() {
                adjustSize(window,bean);
                showKeyBorarIfNeed(window,bean);
                window.getCurrentComponentFocus().get().getComponentTreeObserver().removeTreeLayoutChangedListener(this);
            }
        });
    }

    private static void showKeyBorarIfNeed(Window window, ConfigBean bean) {
        window.setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_UNSPECIFIED);
        Tool.showSoftKeyBoardDelayed(bean.needSoftKeyboard,bean.viewHolder);
        Tool.showSoftKeyBoardDelayed(bean.needSoftKeyboard,bean.customContentHolder);

    }

    private static void setGravity(Window window, ConfigBean bean) {
        window.setLayoutAlignment(bean.gravity);
    }


    private static void setIfTypeToast(Window window, ConfigBean bean) {

    }

    private static void adjustSize(Window window, ConfigBean bean) {
        Component rootView = window.getCurrentComponentFocus().get();
        WindowManager.LayoutConfig wl = window.getLayoutConfig().get();
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(StyledDialog.context);
        DisplayAttributes attrs = display.get().getAttributes();
        int width = attrs.width;
        int height = attrs.height;
        int measuredHeight = rootView.getEstimatedHeight();
        int measuredWidth = rootView.getEstimatedWidth();
        float widthRatio = 0f;
        float heightRatio = 0f;
        if(width > height){//宽屏
            widthRatio = 0.5f;
        }

        //set ratio as user has set
        if(bean.maxWidthPercent > 0 && measuredWidth > bean.maxWidthPercent * width){
            widthRatio = bean.maxWidthPercent;
        }
        if(bean.forceWidthPercent >0 && bean.forceWidthPercent <=1.0f){
            widthRatio = bean.forceWidthPercent;
        }
        if(bean.maxHeightPercent> 0 && measuredHeight > bean.maxHeightPercent * height){
            heightRatio = bean.maxHeightPercent;
        }
        if(bean.forceHeightPercent >0 && bean.forceHeightPercent <=1.0f){
            heightRatio = bean.forceHeightPercent;
        }

        boolean needUpdate = false;
        if(widthRatio >0){
            wl.width = (int) (width * widthRatio);//stretch when the content is not enough,margin when the content is full fill the screen
            needUpdate = true;
        }

        //only work when the content is full fill the screen
        if(heightRatio>0){
            wl.height = (int) (height* heightRatio);
            needUpdate = true;
        }

        if(needUpdate){
            window.setLayoutConfig(wl);
        }

    }
}
